package com.example.scenictourback.service.impl;

import com.example.scenictourback.dto.HotelDTO;
import com.example.scenictourback.dto.RoomDTO;
import com.example.scenictourback.entity.Hotel;
import com.example.scenictourback.entity.Room;
import com.example.scenictourback.entity.ScenicArea;
import com.example.scenictourback.repository.HotelRepository;
import com.example.scenictourback.repository.RoomRepository;
import com.example.scenictourback.repository.ScenicAreaRepository;
import com.example.scenictourback.service.HotelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class HotelServiceImpl implements HotelService {

    @Autowired
    private HotelRepository hotelRepository;
    
    @Autowired
    private ScenicAreaRepository scenicAreaRepository;
    
    @Autowired
    private RoomRepository roomRepository;

    @Override
    public List<HotelDTO> listAllHotels() {
        List<Hotel> hotels = hotelRepository.findAll();
        return HotelDTO.fromEntities(hotels);
    }

    @Override
    public List<HotelDTO> listHotelsByScenicAreaId(Integer scenicAreaId) {
        List<Hotel> hotels = hotelRepository.findByScenicAreaId(scenicAreaId);
        return HotelDTO.fromEntities(hotels);
    }

    @Override
    public HotelDTO getHotelById(Integer id) {
        Hotel hotel = hotelRepository.findById(id)
                .orElseThrow(() -> new RuntimeException("酒店不存在"));
        
        HotelDTO dto = HotelDTO.fromEntity(hotel);
        
        // 查询酒店的所有房间
        List<Room> rooms = roomRepository.findByHotelId(id);
        dto.setRooms(RoomDTO.fromEntities(rooms));
        
        return dto;
    }

    @Override
    public List<HotelDTO> searchHotelsByName(String name) {
        List<Hotel> hotels = hotelRepository.findByNameContaining(name);
        return HotelDTO.fromEntities(hotels);
    }

    @Override
    @Transactional
    public HotelDTO addHotel(Hotel hotel) {
        // 如果关联了景区，检查景区是否存在
        if (hotel.getScenicArea() != null && hotel.getScenicArea().getId() != null) {
            ScenicArea scenicArea = scenicAreaRepository.findById(hotel.getScenicArea().getId())
                    .orElseThrow(() -> new RuntimeException("景区不存在"));
            hotel.setScenicArea(scenicArea);
        } else {
            hotel.setScenicArea(null);
        }
        
        // 保存酒店信息
        Hotel savedHotel = hotelRepository.save(hotel);
        return HotelDTO.fromEntity(savedHotel);
    }

    @Override
    @Transactional
    public HotelDTO updateHotel(Integer id, Hotel hotel) {
        // 检查酒店是否存在
        if (!hotelRepository.existsById(id)) {
            throw new RuntimeException("酒店不存在");
        }
        
        // 如果关联了景区，检查景区是否存在
        if (hotel.getScenicArea() != null && hotel.getScenicArea().getId() != null) {
            ScenicArea scenicArea = scenicAreaRepository.findById(hotel.getScenicArea().getId())
                    .orElseThrow(() -> new RuntimeException("景区不存在"));
            hotel.setScenicArea(scenicArea);
        } else {
            hotel.setScenicArea(null);
        }
        
        // 设置ID
        hotel.setId(id);
        
        // 更新酒店信息
        Hotel updatedHotel = hotelRepository.save(hotel);
        return HotelDTO.fromEntity(updatedHotel);
    }

    @Override
    @Transactional
    public void deleteHotel(Integer id) {
        // 检查酒店是否存在
        if (!hotelRepository.existsById(id)) {
            throw new RuntimeException("酒店不存在");
        }
        
        // 删除酒店
        hotelRepository.deleteById(id);
    }
} 